{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import socket\n",
    "import threading\n",
    "import timeit\n",
    "\n",
    "success = []\n",
    "fail = []\n",
    "port_list = list(range(2000, 3000))\n",
    "address = '127.0.0.1'\n",
    "worker = 16\n",
    "\n",
    "\n",
    "def _connect_test():\n",
    "    s = socket.socket()\n",
    "    while True:\n",
    "        try:\n",
    "            if not port_list:\n",
    "                break\n",
    "            port = port_list.pop()\n",
    "            s.connect((address, port))\n",
    "            print(\"[success] - %s - %s \" % (address, port))\n",
    "            success.append(port)\n",
    "        except socket.error:\n",
    "            print(\"[failed] - %s - %s \" % (address, port))\n",
    "            fail.append(port)\n",
    "\n",
    "\n",
    "def scan_port():\n",
    "    th_ist = []\n",
    "    for th_n in range(worker):\n",
    "        th_ist.append(threading.Thread(target=_connect_test))\n",
    "\n",
    "    for th in th_ist:\n",
    "        th.start()\n",
    "\n",
    "    for th in th_ist:\n",
    "        th.join()\n",
    "    print(success)\n",
    "    print(fail)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    timeit.timeit(scan_port, number=1)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
